package com.yt.calendar.controller;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.yt.calendar.dto.EventDto;
import com.yt.calendar.service.IEventService;
import com.yt.calendar.vo.ResponseVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yt
 * @since 2021-07-16
 */
@RestController
@RequestMapping("/calendar/event")
public class EventController {
    @Resource
    private IEventService iEventService;

    /**
     * 描述:  通过月份查询
     *
     * @param start 开始月份的年月份字符串，例如1月传:202101
     * @param end   结束月份的年月份字符串，例如1月传:202101
     * @return com.yt.calendar.vo.ResponseVo
     * @date: 2021/7/18 0:07
     */
    @GetMapping("/getByMonth")
    public ResponseVo getByMonth(@RequestParam("start") String start, @RequestParam("end") String end) {
        LocalDate startMonth = LocalDateTimeUtil.parseDate(start + "01", "yyyyMMdd");
        LocalDate firstDayOfThisMonth = startMonth.with(TemporalAdjusters.firstDayOfMonth());

        LocalDate endMonth = LocalDateTimeUtil.parseDate(end + "01", "yyyyMMdd");
        LocalDate lastDayOfThisMonth = endMonth.with(TemporalAdjusters.lastDayOfMonth());
        return new ResponseVo(1, iEventService.listByDate(firstDayOfThisMonth, lastDayOfThisMonth));
    }

    /**
     * 描述：保存事件
     *
     * @param eventDto 事件dto对象
     * @return "com.yt.calendar.vo.ResponseVo"
     * @date 2021/7/17 16:29
     */
    @PostMapping("/saveEvent")
    public ResponseVo saveEvent(@RequestBody EventDto eventDto) {
        boolean isSuccess = iEventService.savePlanAndEvent(eventDto);
        return new ResponseVo(isSuccess ? 1 : 0);
    }

    /**
     * 描述：删除事件
     *
     * @param eventDto 事件dto对象, 只要是两个id和一个original
     * @return "com.yt.calendar.vo.ResponseVo"
     * @date 2021/7/17 16:37
     */
    @PostMapping("/deleteEvent")
    public ResponseVo deleteEvent(@RequestBody EventDto eventDto) {
        boolean isSuccess = iEventService.removePlanAndEvent(eventDto);
        return new ResponseVo(isSuccess ? 1 : 0);
    }

    /**
     * 描述: 更新状态
     *
     * @param eventDto 事件dto对象, 只要一个事件id和当前状态
     * @return com.yt.calendar.vo.ResponseVo
     * @date: 2021/7/24 12:03
     */
    @PostMapping("/updateStatus")
    public ResponseVo updateStatus(@RequestBody EventDto eventDto) {
        if (eventDto.getEventId() == null || eventDto.getStatus() == null) {
            return new ResponseVo(0, "更新失败，参数有误");
        }
        boolean isSuccess = iEventService.updateStatus(eventDto);
        return new ResponseVo(isSuccess ? 1 : 0);
    }
}

